//
//  JWSexVC.m
//  jinwang
//
//  Created by jzb on 2018/3/9.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWSexVC.h"

@interface JWSexVC ()
{
    NSString *_sex;
    UIButton *_manBtn;
    UIButton *_WomanBtn;
}
@property (nonatomic,strong) UIButton *preserveBtn;

@end

@implementation JWSexVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"性别";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *preserveItem = [[UIBarButtonItem alloc]initWithCustomView:self.preserveBtn];
    self.navigationItem.rightBarButtonItem = preserveItem;
    
    UILabel *manLabel = [[UILabel alloc]initWithFrame:CGRectMake(45/2, 180/2, 40/2, 40/2)];
    manLabel.text = @"男";
    [self.view addSubview:manLabel];
    
    _manBtn = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth-40, 180/2, 40/2, 40/2)];
    
    _manBtn.tag = 1000;
    
    [_manBtn addTarget:self action:@selector(manBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_manBtn];
    
    UIView *Lineview01 = [[UIView alloc] init];
    Lineview01.frame = CGRectMake(30/2,230/2,kScreenWidth-30,1);
    Lineview01.backgroundColor = kRGBColor(204, 204, 204);
    [self.view addSubview:Lineview01];
    
    UILabel *WomanLabel = [[UILabel alloc]initWithFrame:CGRectMake(45/2, 282/2, 40/2, 40/2)];
    WomanLabel.text = @"女";
    [self.view addSubview:WomanLabel];
    
    _WomanBtn = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth-40, 282/2, 40/2, 40/2)];
    
    _WomanBtn.tag = 1001;
    
    [_WomanBtn addTarget:self action:@selector(manBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_WomanBtn];
    
    UIView *Lineview02 = [[UIView alloc] init];
    Lineview02.frame = CGRectMake(30/2,332/2,kScreenWidth-30,1);
    Lineview02.backgroundColor = kRGBColor(204, 204, 204);
    [self.view addSubview:Lineview02];
    
    if ([k_user_sex integerValue] == 0) {
        
        _sex = @"0";
        [_manBtn setImage:[UIImage imageNamed:@"jw_sex_btn_sel"] forState:UIControlStateNormal];

        [_WomanBtn setImage:[UIImage imageNamed:@"jw_sex_btn"] forState:UIControlStateNormal];
    }else{
        
        _sex = @"1";
        
        [_manBtn setImage:[UIImage imageNamed:@"jw_sex_btn"] forState:UIControlStateNormal];
        
        [_WomanBtn setImage:[UIImage imageNamed:@"jw_sex_btn_sel"] forState:UIControlStateNormal];
    }
    
    
}
#pragma mark -懒加载
- (UIButton *)preserveBtn{
    if (!_preserveBtn) {
        _preserveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _preserveBtn.frame = CGRectMake(0, 0, 44, 44);
        [_preserveBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_preserveBtn addTarget:self action:@selector(preserveButOnclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _preserveBtn;
}

- (void)manBtnOnclick:(UIButton *)sender{
    
    UIButton *manButton = (UIButton *)[self.view viewWithTag:1000];
    UIButton *womanButton = (UIButton *)[self.view viewWithTag:1001];
    
    [manButton setImage:[UIImage imageNamed:@"jw_sex_btn"] forState:UIControlStateNormal];
    [womanButton setImage:[UIImage imageNamed:@"jw_sex_btn"] forState:UIControlStateNormal];
    
    if (sender.tag == 1000) {
        
        [manButton setImage:[UIImage imageNamed:@"jw_sex_btn_sel"] forState:UIControlStateNormal];
        _sex = @"0";
        
    }else{
        
        [womanButton setImage:[UIImage imageNamed:@"jw_sex_btn_sel"] forState:UIControlStateNormal];
       _sex = @"1";
    }
    
}
- (void)preserveButOnclick:(UIButton *)sender{
    
    [[DDNetworkManagerDate makeUrl_setUserInfo_with_token:k_user_token user_nickname:nil user_sex:_sex user_sign:nil user_portrait:nil] post_RequestFinshSuccess:^(id responseObject) {
        
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            [kUserDefaults setObject:_sex forKey:@"user_sex"];
            [self goBack];
        }
        
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];

}

#pragma mark -网络请求

-(void)net_makeUrl_setUserInfo{
    [[DDNetworkManagerDate makeUrl_setUserInfo_with_token:k_user_token user_nickname:nil user_sex:nil user_sign:nil user_portrait:nil] post_RequestFinshSuccess:^(id responseObject) {
        
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
            [self goBack];
        }
        
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
